/*
 * For more information 
 * my blog http://erenha.wordpress.com 
 * my email rn.hindarwan@programmer.net
 */
package com.wordpress.erenha.java.beansbinding.util.converter;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.jdesktop.beansbinding.Converter;

/**
 * Converter for beans binding when source is String and target is double.
 *
 * @author Hindarwan
 */
public class StringToDouble extends Converter<String, Double> {
    
    private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    /**
     * Default String value when get error in conversion
     */
    private String defaultString = "";
    /**
     * Default double value when get error in conversion
     */
    private double defaultDouble = 0;
    public static final String PROP_DEFAULTSTRING = "defaultString";
    public static final String PROP_DEFAULTDOUBLE = "defaultDouble";

    @Override
    public String convertReverse(Double value) {
        String valueString = defaultString;
        try {
            valueString = String.valueOf(value);
        } catch (Exception ex) {
        }
        return valueString;
    }

    @Override
    public Double convertForward(String value) {
        Double valueDouble = defaultDouble;
        try {
            valueDouble = Double.parseDouble(value);
        } catch (Exception ex) {
        }
        return valueDouble;
    }

    /**
     * Get the value of defaultDouble
     *
     * @return the value of defaultDouble
     */
    public double getDefaultDouble() {
        return defaultDouble;
    }

    /**
     * Set the value of defaultDouble
     *
     * @param defaultDouble new value of defaultDouble
     */
    public void setDefaultDouble(double defaultDouble) {
        double oldDefaultDouble = this.defaultDouble;
        this.defaultDouble = defaultDouble;
        propertyChangeSupport.firePropertyChange(PROP_DEFAULTDOUBLE, oldDefaultDouble, defaultDouble);
    }

    /**
     * Get the value of defaultString
     *
     * @return the value of defaultString
     */
    public String getDefaultString() {
        return defaultString;
    }

    /**
     * Set the value of defaultString
     *
     * @param defaultString new value of defaultString
     */
    public void setDefaultString(String defaultString) {
        String oldDefaultString = this.defaultString;
        this.defaultString = defaultString;
        propertyChangeSupport.firePropertyChange(PROP_DEFAULTSTRING, oldDefaultString, defaultString);
    }

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
}
